package cn.com.dashihui.web.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.web.dao.SMS;

public class SMSService {
	
	public boolean sortConfig(Map<String,String> sortMap){
		int batchSize = sortMap.size();
		List<String> sqlList = new ArrayList<String>();
		for(String id : sortMap.keySet()){
			sqlList.add("UPDATE t_sys_sms SET orderNo="+sortMap.get(id)+" WHERE id="+id);
		}
		int[] result = Db.batch(sqlList,batchSize);
		return result.length>0;
	}

	public Page<Record> findByPage(int pageNum, int pageSize,int userid,int RoleId){
		StringBuffer sqlExcept = new StringBuffer("FROM t_sys_sms A ");
		List<Object> params = new ArrayList<Object>();
		Page<Record> records = null;
		if(RoleId==1){
			sqlExcept.append(" ORDER BY orderNo");
		    records= Db.paginate(pageNum, pageSize, "SELECT A.* ", sqlExcept.toString(),params.toArray());
		}else if(RoleId==2){
			Record record=this.findSellerIdByUserId(userid);
			sqlExcept.append("left join t_bus_seller B on A.sellerid=B.id ");
			sqlExcept.append("where A.type=4 and A.sellerid=?");
			params.add(record.getInt("id"));
			sqlExcept.append(" ORDER BY orderNo");
			records= Db.paginate(pageNum, pageSize, "SELECT A.*,B.sellerName ", sqlExcept.toString(),params.toArray());
		}else if(RoleId==3){
			Record record=this.findStoreIdByUserId(userid);
			sqlExcept.append("left join t_bus_seller B on A.sellerid=B.id left join t_dict_store C on A.storeid=C.id ");
			sqlExcept.append("where A.type=5 and A.storeid=?");
			params.add(record.getInt("id"));
			sqlExcept.append(" ORDER BY orderNo");
			records= Db.paginate(pageNum, pageSize, "SELECT A.*,B.sellerName,C.title ", sqlExcept.toString(),params.toArray());
		}else{
			sqlExcept.append("where A.type=6");
			sqlExcept.append(" ORDER BY orderNo");
		    records= Db.paginate(pageNum, pageSize, "SELECT A.* ", sqlExcept.toString(),params.toArray());
		}
		return records;
	}
	
	public boolean addSMS(SMS newObject){
		return newObject.save();
	}
	
	public boolean delConfig(int id){
		return SMS.me().deleteById(id);
	}
	
	public boolean editConfig(SMS object){
		return object.update();
	}
	
	public SMS findById(int id){
		return SMS.me().findFirst("SELECT * FROM t_sys_sms WHERE id=?",id);
	}
	
	public SMS findByPhone(String phone){
		return SMS.me().findFirst("SELECT * FROM t_sys_sms WHERE phone=?",phone);
	}
	
	public List<SMS> findAllSMS(){
		return SMS.me().find("SELECT * FROM t_sys_sms c ORDER BY c.orderNo");
	}
	public List<SMS> findAllSMSBySellerId(int id){
		return SMS.me().find("SELECT * FROM t_sys_sms c where type=4 and sellerid=? ORDER  BY c.orderNo",id);
	}
	public List<SMS> findAllSMSByStoreId(int id){
		return SMS.me().find("SELECT * FROM t_sys_sms c where type=5 and storeid=? ORDER  BY c.orderNo",id);
	}
	public List<SMS> findAllSMSByUserId(){
		return SMS.me().find("SELECT * FROM t_sys_sms c where type=6 ORDER  BY c.orderNo");
	}
	/**
	 * 根据userid,获取角色
	 * @param userid
	 * @return
	 */
	public int findByRoleId(int userid){
		return Db.queryInt("SELECT C.id  from t_auth_admin_seller as A inner JOIN  t_auth_admin_roles_seller as B ON A.id=B.userid  INNER JOIN t_auth_roles_seller AS C ON B.roleid = C.id where B.userid=?" ,userid);
	}
	/**
	 * 根据userid,获取所属公司的sellerId
	 * @param userid
	 * @return
	 */
	public Record findSellerIdByUserId(int userid){
		return Db.findFirst("SELECT id,sellerName from  t_bus_seller  where userid= ?" ,userid);
	}
	public Record findStoreIdByUserId(int userid){
		return Db.findFirst("SELECT B.id,B.title,C.id as sellerid  from  t_auth_admin_seller A left join t_dict_store B on A.storeid=B.storeid "
				+ "left join t_bus_seller C on B.sellerId=C.sellerId where A.id= ?" ,userid);
	}
   
}
